package com.bank.ui.admin;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import com.bank.dao.CardDAO;
import com.bank.entity.Card;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.sql.SQLException;

public class AddCardDialog extends JDialog {
    public AddCardDialog() {
        setTitle("添加银行卡");
        setSize(300, 200);
        setModal(true);
        setLocationRelativeTo(null);
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(5, 2));
        JLabel cardIDLabel = new JLabel("卡号:");
        JTextField cardIDField = new JTextField();
        JLabel savingIDLabel = new JLabel("存款类型:");
        JTextField savingIDField = new JTextField();
        JLabel openDateLabel = new JLabel("开户日期 (yyyy-MM-dd):");
        JTextField openDateField = new JTextField();
        JLabel openMoneyLabel = new JLabel("开户金额:");
        JTextField openMoneyField = new JTextField();
        JLabel customerIDLabel = new JLabel("客户编号:");
        JTextField customerIDField = new JTextField();
        panel.add(cardIDLabel);
        panel.add(cardIDField);
        panel.add(savingIDLabel);
        panel.add(savingIDField);
        panel.add(openDateLabel);
        panel.add(openDateField);
        panel.add(openMoneyLabel);
        panel.add(openMoneyField);
        panel.add(customerIDLabel);
        panel.add(customerIDField);
        JButton saveButton = new JButton("保存");
        saveButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String cardID = cardIDField.getText();
                String savingIDStr = savingIDField.getText();
                String openDateStr = openDateField.getText();
                String openMoneyStr = openMoneyField.getText();
                String customerIDStr = customerIDField.getText();
              //输入验证：
                if (cardID.isEmpty() || savingIDStr.isEmpty() || openDateStr.isEmpty() ||
                        openMoneyStr.isEmpty() || customerIDStr.isEmpty()) {
                    JOptionPane.showMessageDialog(AddCardDialog.this, "请填写所有必填项", "错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }

                // 新增：卡号格式验证
                if (cardID.length() != 16 || !cardID.matches("\\d{16}")) {
                    JOptionPane.showMessageDialog(AddCardDialog.this,
                            "卡号必须为16位数字！", "卡号格式错误", JOptionPane.ERROR_MESSAGE);
                    return;
                }
              //卡号唯一性检查：
                try {
                    // 新增：卡号唯一性验证
                    CardDAO cardDAO = new CardDAO();
                    if (cardDAO.cardExists(cardID)) {
                        JOptionPane.showMessageDialog(AddCardDialog.this,
                                "该卡号已存在！", "卡号重复", JOptionPane.ERROR_MESSAGE);
                        return;
                    }

                    int savingID = Integer.parseInt(savingIDStr);
                    double openMoney = Double.parseDouble(openMoneyStr);
                    int customerID = Integer.parseInt(customerIDStr);

                    // 转换日期格式
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    Date openDate = sdf.parse(openDateStr);
                   //数据转换与封装：
                    Card card = new Card();
                    card.setCardID(cardID);
                    card.setSavingID(savingID);
                    card.setOpenDate(openDate);
                    card.setOpenMoney(openMoney);
                    card.setCustomerID(customerID);
                    card.setIsReportLoss("否"); // 默认未挂失
                    card.setPassword("123456"); // 默认密码
                   //数据库操作：
                    cardDAO.addCard(card);
                    JOptionPane.showMessageDialog(AddCardDialog.this, "银行卡添加成功", "成功", JOptionPane.INFORMATION_MESSAGE);
                    dispose();
                } catch (NumberFormatException ex) {
                    JOptionPane.showMessageDialog(AddCardDialog.this, "请输入正确的数字格式", "错误", JOptionPane.ERROR_MESSAGE);
                } catch (ParseException ex) {
                    JOptionPane.showMessageDialog(AddCardDialog.this, "请输入正确的日期格式 (yyyy-MM-dd)", "错误", JOptionPane.ERROR_MESSAGE);
                } catch (SQLException ex) {
                    ex.printStackTrace();
                    JOptionPane.showMessageDialog(AddCardDialog.this,
                            "数据库错误: " + ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
                } catch (Exception ex) {
                    ex.printStackTrace();
                    JOptionPane.showMessageDialog(AddCardDialog.this, "银行卡添加失败", "错误", JOptionPane.ERROR_MESSAGE);
                }
            }
        });
        add(panel, BorderLayout.CENTER);
        add(saveButton, BorderLayout.SOUTH);
    }
}